/**
 * 场景操作的相关方法
 */

var baseURL =  localStorage.getItem("baseURL");

//firstSortObject 为echart排序组件全局缓存初始变量，
var firstSortObject={}; 
//firstBarObject 为echart柱状图全局缓存初始变量，
var firstBarObject={};
!function(win){
    "use strict";
    var ModuleAnimation = function(){
        /*this.temp_ldt_value;//流动条
        this.temp_sc_value;//水池
        this.temp_wzV_value;//文字value
        this.temp_v_value;//值
        this.temp_echartWdj_value;//echart温度计
        this.temp_echartYbp_value;//echart仪表盘
        this.temp_echaartXt_value;//echart线图
        this.temp_echartSc_value;//echart水池
        this.temp_ztWz_value;//状态文字
        this.temp_ztD_value;//状态灯
        this.temp_xdys_value;//线段颜色
        this.temp_kg_value;//开关
        this.temp_zttp_value;//状态图片
        this.temp_xz_value;//旋转
        this.temp_yc_value;//影藏
        this.temp_ss_value;//闪烁*/


    }
    ModuleAnimation.prototype={
        constructor:ModuleAnimation,
        /**
         * 流动水管，线条的动画方法
         */
        setLineValue:function(moduleObject,valueObject) {
            var groupObj = moduleObject;
            var layerObj = groupObj.getLayer();
            var whereStr = groupObj.attrs.where;
            var keyValue ='';
            for (var item in valueObject) {
                // item;//得到键/
                // valueObj[item];//得到键对应的值
                keyValue =  valueObject[item];
            }
            if(groupObj.temp_ldt_value == keyValue){
                return;
            }else{
                groupObj.temp_ldt_value=keyValue;
            }


            var lineObj = groupObj.find('.dynamicLine')[0];

            var whereObj = eval("("+whereStr+")");
            for (var i = 0; i < whereObj.length; i++) {
                var whereObjItem = whereObj[i];
                var direction = whereObjItem.direction;

                var min=whereObjItem.where['min'];
                var max=whereObjItem.where['max'];
                if(Number(keyValue)>=Number(min)&&Number(keyValue)<=Number(max)){
                     if(direction==1){//正向流动

                         if (groupObj.hasOwnProperty('anim')) { //必须先停止
                             groupObj.anim.stop();
                         }
                             groupObj.anim = new Konva.Animation(function (frame) {
                                 lineObj.dashOffset(-(frame.time / 20));
                             }, layerObj);
                             groupObj.anim.start();



                     }
                    if(direction==11){//快速正向流动

                        if (groupObj.hasOwnProperty('anim')) { //必须先停止
                            groupObj.anim.stop();
                        }
                        groupObj.anim = new Konva.Animation(function (frame) {
                            lineObj.dashOffset(-(frame.time / 10));
                        }, layerObj);
                        groupObj.anim.start();



                    }
                     if(direction==2){//反向流动

                         if (groupObj.hasOwnProperty('anim')) { //必须先停止
                             groupObj.anim.stop();
                         }
                             groupObj.anim = new Konva.Animation(function (frame) {
                                 lineObj.dashOffset(frame.time / 20);
                             }, layerObj);
                             groupObj.anim.start();



                     }
                    if(direction==22){//快速反向流动

                        if (groupObj.hasOwnProperty('anim')) { //必须先停止
                            groupObj.anim.stop();
                        }
                        groupObj.anim = new Konva.Animation(function (frame) {
                            lineObj.dashOffset(frame.time / 10);
                        }, layerObj);
                        groupObj.anim.start();



                    }
                     if(direction==3){//停止流动
                         if (groupObj.hasOwnProperty('anim')) { //判断对象是否存在 如果存在就不要 添加任务了
                             groupObj.anim.stop();
                         }

                     }

                     break;
                }else{
                    if (groupObj.hasOwnProperty('anim')) { //判断对象是否存在 如果存在就不要 添加任务了
                        groupObj.anim.stop();
                    }
                }

            }




          /*  for (var item in valueObject) {
                whereStr = whereStr.replace(eval("/" + item + ":/g"), valueObject[item])
            }
            var ismz = false;//是否满足报警条件
            var whereArray = eval(whereStr);
            for (var i = 0; i < whereArray.length; i++) {
                if (eval(whereArray[i])) {// 水管流动条件

                    if (groupObj.hasOwnProperty('anim')) { //判断对象是否存在 如果存在就不要 添加任务了
                        if (groupObj.anim.isRunning() == false) {//没有运行启动
                            groupObj.anim.start();
                        }
                    } else {

                        groupObj.anim = new Konva.Animation(function (frame) {
                            lineObj.dashOffset(-(frame.time / 40));
                        }, layerObj);
                        groupObj.anim.start();

                    }

                    ismz = true;

                    break;

                }
            }
            if (ismz == false) {//

                try {//如果第一次就不满足动画，那么这里会抛出异常
                    groupObj.anim.stop();
                    //layerObj.draw();
                } catch (e) {

                }
            }*/

        },
        /**
         * 设置水池对象
         * @param moduleObject
         * @param valueObject
         */
        setPoolValue:function(moduleObject,valueObject) {

            var poolObj = moduleObject;
            var poolo = poolObj.find('.pool_background')[0]; //水池对象
            var watero = poolObj.find('.pool_water')[0]; //水对象
            var poolHeight = poolObj.attrs.poolHight;

            var waterHeight=0;
            //循环valueObj 将 循环valueObj 里面的值 替换到 whereStr 中

            for (var item in valueObject) {
                // item;//得到键/
                // valueObj[item];//得到键对应的值
                waterHeight =  valueObject[item];
            }

            if(poolObj.temp_sc_value == waterHeight){
                return;
            }else{
                poolObj.temp_sc_value=waterHeight;
            }



            if(waterHeight=="undefined"){
                return;
            }

            //水占水池的比例  （实际）
            var waterProportion = waterHeight/poolHeight;
            var poolObjy = poolo.getY();// 水池 y
            var poolObjHeight = poolo.getHeight();//水池 高
            //水对象的高
            var waterHeightTemp = poolObjHeight*waterProportion;

            if(waterHeightTemp>poolObjHeight){// 水的高 大于水池的高，取水池的高
                waterHeightTemp=poolObjHeight;
            }
            if(waterHeightTemp<=0){// 水的高 大于水池的高，取水池的高
                waterHeightTemp=0;
            }

            var watery = poolObjy+poolObjHeight-waterHeightTemp; //水池 y + 水池的高 - 水的高度 = 水的y


            watero.tween = new Konva.Tween({ // 设置水的补间动画 （以水的y的变化来做动画）
                node: watero,
                duration:0.5,
                y: watery,
                height: waterHeightTemp,
                onFinish: function() {

                }
            });
            watero.tween.play();
        },
        /**
         * 设置文字-value的值
         */
        setTextValue:function(moduleObject,valueObject) {
            var layerObj = moduleObject.getLayer();
            var groupObj = moduleObject;
            var TextValue = groupObj.find('.complexTextValue')[0];



            var valueStr ="";
            for (var item in valueObject) {
                valueStr =  valueObject[item];
				//console.log(valueStr);
            }

            if( groupObj.temp_wzV_value == valueStr){
                return;
            }else{
                groupObj.temp_wzV_value=valueStr;
            }
            // TextValue.text(valueStr+groupObj.attrs.dw);
            // TextValue.attrs.value=valueStr;

            var valueStrF=valueStr;
            if(valueStrF != "" && valueStrF !=null&&!isNaN(valueStrF)){
                var valueZoomConfig = groupObj.attrs.valueZoomConfig;// 获取放大缩小配置
                if(typeof(valueZoomConfig)!="undefined"){
                    var valueZoomConfigObjArr = eval("("+valueZoomConfig+")");
                    var valueZoomConfigObj=valueZoomConfigObjArr[0];
                    var zoomSize=valueZoomConfigObj.zoomSize;  // 放大倍数
                    var decimalDigits=valueZoomConfigObj.decimalDigits;//小数点
                    valueStrF = $_floatMul(valueStr,zoomSize);
                    valueStrF=$_changeTwoDecimal_f(valueStrF,Number(decimalDigits),'Y');
                }
            }
            var unit=groupObj.attrs.unit;
            if(typeof(unit)=="undefined"){
                unit="";
            }
            TextValue.text(valueStrF+unit);
            var rectValue = groupObj.find('.rectValue')[0];
            rectValue.width(TextValue.getWidth());
            rectValue.height(TextValue.getHeight());
            //layerObj.draw();
        },
        /**
         * 设置value的值
         */
        setTValue:function(moduleObject,valueObject) {
            var layerObj = moduleObject.getLayer();
            var groupObj = moduleObject;
            var TextValue = groupObj.find('.complexTextValue')[0];

            var valueStr ="";
            for (var item in valueObject) {
                valueStr =  valueObject[item];
            }

            if( groupObj.temp_v_value == valueStr){
                return;
            }else{
                groupObj.temp_v_value=valueStr;
            }
            var valueStrF=valueStr;
            if(valueStrF != "" && valueStrF !=null&&!isNaN(valueStrF)) {
                var valueZoomConfig = groupObj.attrs.valueZoomConfig;// 获取放大缩小配置
                if (typeof (valueZoomConfig) != "undefined") {
                    var valueZoomConfigObjArr = eval("(" + valueZoomConfig + ")");
                    var valueZoomConfigObj = valueZoomConfigObjArr[0];
                    var zoomSize = valueZoomConfigObj.zoomSize;  // 放大倍数
                    var decimalDigits = valueZoomConfigObj.decimalDigits;//小数点
                    valueStrF = $_floatMul(valueStr, zoomSize);
                    valueStrF = $_changeTwoDecimal_f(valueStrF, Number(decimalDigits), 'Y');
                }
            }
            var unit=groupObj.attrs.unit;
            if(typeof(unit)=="undefined"){
                unit="";
            }
            TextValue.text(valueStrF+unit);

            try{
            var text_border = groupObj.find('.text_border')[0];
            text_border.width(TextValue.getWidth());
            text_border.height(TextValue.getHeight());
            }catch(e){

            }
            //layerObj.draw();
        },
        /**
         * 设置仪表盘的值
         */
        setEchart_GaugeValue:function(moduleObject,valueObject) {
            
            var moduleType = moduleObject.attrs.moduleType;
            if("ECHART_THERMOMETER"==moduleType){//温度计
                var valueStr =0;
                for (var item in valueObject) {
                    valueStr =  valueObject[item];
                }

                if( moduleObject.temp_echartWdj_value == valueStr){
                    return;
                }else{
                    moduleObject.temp_echartWdj_value=valueStr;
                }

                var maxValue =moduleObject.attrs.maxValue;
                var minValue =moduleObject.attrs.minValue;



                var moduleObjectId = moduleObject.id();

                var echartDiv = document.getElementById(moduleObjectId);
                var myChart = echarts.init(echartDiv);
                var echart_option = eval(moduleObject.attrs.echartURL+"("+valueStr+","+maxValue+","+minValue+")");

                myChart.setOption(echart_option);

            }
            if("ECHART_GAUGE"==moduleType){//仪表盘
                var moduleObjectId = moduleObject.id();
                var valueStr ="";
                for (var item in valueObject) {
                    valueStr =  valueObject[item];
                }

                if( moduleObject.temp_echartYbp_value == valueStr){
                    return;
                }else{
                    moduleObject.temp_echartYbp_value=valueStr;
                }

                var echartDiv = document.getElementById(moduleObjectId);
                var echartObject =echarts.getInstanceByDom(echartDiv);//通过id获取echart实例
                var echartOption=echartObject.getOption();
                var seriesData = echartOption.series;
                for(var i=0; i<seriesData.length;i++){//有的表盘是两个叠加，所以要循环
                    seriesData[i].data[0].value=valueStr;
                }
                echartObject.setOption({
                    series: seriesData
                });
            }
            if("ECHART_realTimeLine"==moduleType){//实时则线图
                var moduleObjectId = moduleObject.id();
                var valueStr ="";
                for (var item in valueObject) {
                    valueStr =  valueObject[item];
                }
                var historyCount = moduleObject.attrs.historyCount;

                var echartDiv = document.getElementById(moduleObjectId);
                var echartObject =echarts.getInstanceByDom(echartDiv);//通过id获取echart实例
                var echartOption=echartObject.getOption();
                var seriesData = echartOption.series[0].data;
                //debugger;
                var now = new Date();
                var dataOneArray={
                    value: [
                        now.getFullYear()+"-"+$_check(now.getMonth()+1)+"-"+$_check(now.getDate())+" "+$_check(now.getHours())+":"+$_check(now.getMinutes())+":"+$_check(now.getSeconds()),
                        valueStr
                    ]

                };
                if(seriesData.length>=historyCount){
                    seriesData.shift();// 去掉第一个历史数据
                }


                seriesData.push(dataOneArray);




                echartObject.setOption({
                    series: [{
                        data: seriesData
                    }]
                });


            }
            if("ECHART_realTimeLineNew"==moduleType){//实时则线图 新
                var moduleObjectId = moduleObject.id();
                var historyCount = moduleObject.attrs.historyCount;

                var echartDiv = document.getElementById(moduleObjectId);
                var echartObject =echarts.getInstanceByDom(echartDiv);//通过id获取echart实例
                var echartOption=echartObject.getOption();

                var i=0;
                for (var item in valueObject) {
                    var valueStr =  valueObject[item];
                    echartOption.series[i].data.push(valueStr);
                    if(echartOption.series[i].data.length>historyCount){
                        echartOption.series[i].data.shift();// 去掉第一个历史数据
                    }
                    i++;
                }
                var now = new Date();
                echartOption.xAxis[0].data.push($_check(now.getHours())+":"+$_check(now.getMinutes())+":"+$_check(now.getSeconds()));
                if(echartOption.xAxis[0].data.length>historyCount){
                    echartOption.xAxis[0].data.shift();// 去掉第一个历史数据
                }
                echartObject.setOption(echartOption);
            }
            if("ECHART_realTimeBarGraph"==moduleType){//实时柱状图
                var moduleObjectId = moduleObject.id();
                var historyCount = moduleObject.attrs.historyCount;

                var echartDiv = document.getElementById(moduleObjectId);
                var echartObject =echarts.getInstanceByDom(echartDiv);//通过id获取echart实例
                var echartOption=echartObject.getOption();

                var series_data=new Array();
                setTimeout(function(){
                //判断对象的属性个数
                //console.log(Object.keys(valueObject).length);
                //console.log(valueObject);
                	if(Object.keys(valueObject).length==echartOption.xAxis[0].max+1){
                		firstBarObject = Object.assign({},valueObject);
                		//console.log(firstBarObject);
                		for (var item in valueObject) {
                			var valueStr =  valueObject[item];
                			series_data.push(valueStr);
                		}
                		//console.log(series_data);
                		echartOption.series[0].data=series_data;
                		echartObject.setOption(echartOption);
                	}else if(Object.keys(valueObject).length<echartOption.xAxis[0].max+1){
                		if(Object.keys(valueObject).length && Object.keys(firstBarObject).length){
                			for (var key in firstBarObject) {
                				if(key in valueObject){
                					firstBarObject[key]=valueObject[key]
                				}
                			}
                			for (var item in firstBarObject) {
                				var valueStr =  firstBarObject[item];
                				series_data.push(valueStr);
                			}
                			//console.log(firstBarObject);
                			echartOption.series[0].data=series_data;
                			echartObject.setOption(echartOption);
                		}
                	}
                },500)
            }
			if("ECHART_realTimeSortBar"==moduleType){//横条排序图
			    var moduleObjectId = moduleObject.id();
			    var historyCount = moduleObject.attrs.historyCount;
			
			    var echartDiv = document.getElementById(moduleObjectId);
			    var echartObject =echarts.getInstanceByDom(echartDiv);//通过id获取echart实例
			    var echartOption=echartObject.getOption();
			
			    var series_data=new Array();
				setTimeout(function(){
				//判断对象的属性个数
				//console.log(Object.keys(valueObject).length);
				//console.log(valueObject);
					if(Object.keys(valueObject).length==echartOption.yAxis[0].max+1){
						firstSortObject = Object.assign({},valueObject);
						//console.log(firstSortObject);
						for (var item in valueObject) {
							var valueStr =  valueObject[item];
							series_data.push(valueStr);
						}
						//console.log(series_data);
						echartOption.series[0].data=series_data;
						echartObject.setOption(echartOption);
					}else if(Object.keys(valueObject).length<echartOption.yAxis[0].max+1){
						if(Object.keys(valueObject).length && Object.keys(firstSortObject).length){
							for (var key in firstSortObject) {
								if(key in valueObject){
									firstSortObject[key]=valueObject[key]
								}
							}
							for (var item in firstSortObject) {
								var valueStr =  firstSortObject[item];
								series_data.push(valueStr);
							}
							//console.log(firstSortObject);
							echartOption.series[0].data=series_data;
							echartObject.setOption(echartOption);
						}
					}
				},1000)
			}
            if("ECHART_realTimePieGraph"==moduleType){//实时饼图
                var moduleObjectId = moduleObject.id();
                var historyCount = moduleObject.attrs.historyCount;

                var echartDiv = document.getElementById(moduleObjectId);
                var echartObject =echarts.getInstanceByDom(echartDiv);//通过id获取echart实例
                var echartOption=echartObject.getOption();

                var series_data=new Array();
                var i=0;
                for (var item in valueObject) {
                    var valueStr =  valueObject[item];
                    echartOption.series[0].data[i].value=valueStr;
                    i++;
                }
                echartObject.setOption(echartOption);
            }
            if("ECHART_realTimeCircularGraph"==moduleType){//实时环形图
                var moduleObjectId = moduleObject.id();
                var historyCount = moduleObject.attrs.historyCount;

                var echartDiv = document.getElementById(moduleObjectId);
                var echartObject =echarts.getInstanceByDom(echartDiv);//通过id获取echart实例
                var echartOption=echartObject.getOption();

                var series_data=new Array();
                var i=0;
                for (var item in valueObject) {
                    var valueStr =  valueObject[item];
                    echartOption.series[0].data[i].value=valueStr;
                    i++;
                }
                echartObject.setOption(echartOption);
            }
            if("ECHART_Pool"==moduleType){//水池
                var valueStr =0;
                for (var item in valueObject) {
                    valueStr =  valueObject[item];
                }
                if( moduleObject.temp_echartSc_value == valueStr){
                    return;
                }else{
                    moduleObject.temp_echartSc_value=valueStr;
                }
                var poolHight =moduleObject.attrs.poolHight;
				//var poolValue=valueStr;
                var poolValue =  $_toDecimal(valueStr/poolHight);
				if($_toDecimal(valueStr/poolHight)<0){
					poolValue=0;
				}
                var moduleObjectId = moduleObject.id();
                var echartDiv = document.getElementById(moduleObjectId);
                var echartObject =echarts.getInstanceByDom(echartDiv);//通过id获取echart实例
                var echartOption=echartObject.getOption();
                echartOption.series[0].data[0]=poolValue;
				echartOption.series[0].label.formatter=function(param) {
					if(valueStr<0){
						return 0;
					}else{
						return  valueStr;
				    }
				}
                echartObject.setOption(echartOption);
            }
			if("ECHART_Pool2"==moduleType){//水池
			    var valueStr =0;
			    for (var item in valueObject) {
			        valueStr =  valueObject[item];
			    }
			    if( moduleObject.temp_echartSc_value == valueStr){
			        return;
			    }else{
			        moduleObject.temp_echartSc_value=valueStr;
			    }
			    var poolHight =moduleObject.attrs.poolHight;
			    var poolValue =  $_toDecimal(valueStr/poolHight)*100;
			    var moduleObjectId = moduleObject.id();
			    var echartDiv = document.getElementById(moduleObjectId);
			    var echartObject =echarts.getInstanceByDom(echartDiv);//通过id获取echart实例
			    var echartOption=echartObject.getOption();
			
			    echartOption.series[1].data[0].value=poolValue;
			
			    echartOption.series[4].data[0]=poolValue;
			    echartObject.setOption(echartOption);
			}
        },
        
		setStatusValue:function(moduleObject,valueObject){
            var layerObj = moduleObject.getLayer();
            var groupObj = moduleObject;

            var whereStr = groupObj.attrs.where;
            var keyValue ="";
            for (var item in valueObject) {
                // item;//得到键/
                // valueObj[item];//得到键对应的值
                keyValue =  valueObject[item];
            }

            if( groupObj.temp_ztWz_value == keyValue){
                return;
            }else{
                groupObj.temp_ztWz_value=keyValue;
            }

            var TextValue = groupObj.find('.complexTextValue')[0];

            var whereObj = eval("("+whereStr+")");
            for (var i = 0; i < whereObj.length; i++) {
                var whereObjItem = whereObj[i];
                var description = whereObjItem.description;

                var min=whereObjItem.where['min'];
                var max=whereObjItem.where['max'];
                if(min!=""||max!="") {
                    if (Number(keyValue) >= Number(min) && Number(keyValue) <= Number(max)) {
                        TextValue.text(description);
                        break;
                    }
                }

            }

            layerObj.draw();
        },
        /**
         * 设置状态灯
         * @param moduleObject
         * @param valueObject
         */
        setStatusLight:function(moduleObject,valueObject){
            var layerObj = moduleObject.getLayer();
            var groupObj = moduleObject;

            var whereStr = groupObj.attrs.where;
            var keyValue ="";
            for (var item in valueObject) {
                // item;//得到键/
                // valueObj[item];//得到键对应的值
                keyValue =  valueObject[item];
            }
            if( groupObj.temp_ztD_value == keyValue){
                return;
            }else{
                groupObj.temp_ztD_value=keyValue;
            }


            var light  =groupObj.findOne('.Light'); //灯

            var whereObj = eval("("+whereStr+")");
            for (var i = 0; i < whereObj.length; i++) {
                var whereObjItem = whereObj[i];
                var color = whereObjItem.color;

                var min=whereObjItem.where['min'];
                var max=whereObjItem.where['max'];
                if(min!=""||max!=""){
                    if(Number(keyValue)>=Number(min)&&Number(keyValue)<=Number(max)){
                        //light.setFillRadialGradientColorStops([0, '#f1f1f1',0.7, color,1, '#f1f1f1']);
						light.setFillRadialGradientColorStops([0, '#f1f1f1',0.9, color]);
                        break;
                    }
                }

            }

            layerObj.draw();
        },
		
		//设置复位按钮的状态颜色
		setResetLight: function(moduleObject, valueObject) {
		    // var layerObj = moduleObject.getLayer();
		    var groupObj = moduleObject;
		    var type = groupObj.attrs.moduleType;
			var oneColor = groupObj.attrs.oneColor; //置1背景色
		    var zeroColor = groupObj.attrs.zeroColor; //置零背景色
		    var keyValue='';
			for (var item in valueObject) {
				// item;//得到键/
				// valueObj[item];//得到键对应的值
				keyValue =  valueObject[item];
			}
		    //console.log(keyValue);
		    if (type == 'RESET') {
		        var light = groupObj.findOne('.buttonRect'); //复位
		        if (keyValue == 0) {
		            light.setFillLinearGradientColorStops([0, zeroColor, 0.5, "#ffffff", 1, zeroColor]);
		        } else {
		            light.setFillLinearGradientColorStops([0, oneColor, 0.5, "#ffffff", 1, oneColor]);
		        }
		    }
		
		    //}
		
		    //layerObj.draw();
		},
        /**
         * 设置线条的状态颜色
         * @param moduleObject
         * @param valueObject
         */
        setStatusLine:function(moduleObject,valueObject){
            var layerObj = moduleObject.getLayer();
            var groupObj = moduleObject;

            var whereStr = groupObj.attrs.where;
            var keyValue ="";
            for (var item in valueObject) {
                // item;//得到键/
                // valueObj[item];//得到键对应的值
                keyValue =  valueObject[item];
            }

            if( groupObj.temp_xdys_value == keyValue){//当前的值和上一次的值如果是一样的，就不要在此渲染了，可以节省资源
                return;
            }else{
                groupObj.temp_xdys_value=keyValue;
            }

            var line1  =groupObj.findOne('.line1'); //线条

            var whereObj = eval("("+whereStr+")");
            for (var i = 0; i < whereObj.length; i++) {
                var whereObjItem = whereObj[i];
                var color = whereObjItem.color;

                var min=whereObjItem.where['min'];
                var max=whereObjItem.where['max'];
                if(min!=""||max!=""){
                if(Number(keyValue)>=Number(min)&&Number(keyValue)<=Number(max)){
                    line1.stroke(color);
                    break;
                }
                }

            }

            //layerObj.draw();
        },
        /**
         * 设置二次接线的颜色  只要是接线图，里面所有的元素设置 线框的颜色
         * @param moduleObject
         * @param valueObject
         */
        setStatusElectric:function(moduleObject,valueObject){
            var layerObj = moduleObject.getLayer();
            var groupObj = moduleObject;

            var whereStr = groupObj.attrs.where;
            var OnOffState = groupObj.attrs.OnOffState;// 开合状态条件。里面包含了颜色
            var keyValue ="";
            for (var item in valueObject) {
                // item;//得到键/
                // valueObj[item];//得到键对应的值
                keyValue =  valueObject[item];
            }

            if( groupObj.temp_xkys_value == keyValue){//当前的值和上一次的值如果是一样的，就不要在此渲染了，可以节省资源
                return;
            }else{
                groupObj.temp_xkys_value=keyValue;
            }

            //遍历里面所有的元素，设置 stroke
            var groupChildren = groupObj.getChildren();

            if(typeof(OnOffState)!="undefined"){//只有开合状态的电力组件才有这个属性
                var whereObj = eval("("+OnOffState+")");// 开合状态条件
                for (var i = 0; i < whereObj.length; i++) {
                    var whereObjItem = whereObj[i];
                    var color = whereObjItem.color;
                    var textState = whereObjItem.textState;

                    var min=whereObjItem.where['min'];
                    var max=whereObjItem.where['max'];
                    if(Number(keyValue)>=Number(min)&&Number(keyValue)<=Number(max)){
                        // 设置电力组件里面元素的颜色
                        for (var z = 0; z < groupChildren.length; z++) {
                            var childObj = groupChildren[z];
                            childObj.stroke(color);

                            if(childObj.attrs.isStrockColorTofill==true){//边框颜色变的时候，如果该元素的isStrockColorTofill=true 那么填充也要跟着边框一起变颜色
                                childObj.setAttr('fill',color);
                            }
                        }

                        //设置电力组件的开合状态
                        var khObj = moduleObject.find('.khys');//找到开合元素
                        var onoffXY= moduleObject.attrs.onoffXY;// 拿到开合坐标
                        if(textState==='合'){
                            khObj.points(onoffXY.on);

                        }else if(textState==='开'){
                            khObj.points(onoffXY.off);
                        }



                        break;
                    }

                }
            }else{

                var whereObj = eval("("+whereStr+")");
                for (var i = 0; i < whereObj.length; i++) {
                    var whereObjItem = whereObj[i];
                    var color = whereObjItem.color;

                    var min=whereObjItem.where['min'];
                    var max=whereObjItem.where['max'];
                    if(Number(keyValue)>=Number(min)&&Number(keyValue)<=Number(max)){
                        // 设置电力组件里面元素的颜色
                        for (var z = 0; z < groupChildren.length; z++) {
                            var childObj = groupChildren[z];
                            childObj.stroke(color);
                            if(childObj.attrs.isStrockColorTofill==true){//边框颜色变的时候，如果该元素的isStrockColorTofill=true 那么填充也要跟着边框一起变颜色
                                childObj.setAttr('fill',color);
                            }
                        }
                        break;
                    }

                }
            }
            //layerObj.draw();
        },
        /**
         *设置开关
         * @param moduleObject
         * @param valueObject
         */
        setSwitchValue:function(moduleObject,valueObject){
            var layerObj = moduleObject.getLayer();
            var groupObj = moduleObject;

            var whereStr = groupObj.attrs.where;
            var dataKey = "";
            var keyValue ="";
            for (var item in valueObject) {
                // item;//得到键/
                // valueObj[item];//得到键对应的值
                keyValue =  valueObject[item];
                dataKey = item;
            }
            if( groupObj.temp_kg_value == keyValue){
                return;
            }else{
                groupObj.temp_kg_value=keyValue;
            }

            var whereObj = eval("("+whereStr+")");
            for (var i = 0; i < whereObj.length; i++) {
                var whereObjItem = whereObj[i];
                var status = whereObjItem.status;
                var command = whereObjItem.command;
                var icon = whereObjItem.icon;

                if(keyValue==status){
                    //删除原有的 image 组件。
                    var nodes = moduleObject.find('Image');
                    nodes.each(function(node) {
                        node.destroy();
                    });
                    var urlImage = icon;
                    Konva.Image.fromURL(urlImage, function(imageNode){
                        imageNode.name("myShape");
                        imageNode.setAttr("offset",{x: imageNode.width() / 2,y: imageNode.height() / 2,});
                        moduleObject.add(imageNode);
                        layerObj.draw();
                    },"config/gyyj/noimg.svg");

                    // 设置当前开关对于的点击事件

                    moduleObject.off('mousedown touchstart');
                    moduleObject.off('mouseup touchend');
                    moduleObject.off('mouseover');
                    moduleObject.off('mouseout');
                    moduleObject.off('click tap');
                    moduleObject.on('mousedown touchstart', function (evt) {
                    });
                    moduleObject.on('mouseup touchend', function (evt) {
                    });
                    moduleObject.on('mouseover', function() {
                        document.body.style.cursor = 'pointer';
                    });
                    moduleObject.on('mouseout', function() {
                        document.body.style.cursor = 'default';
                    });
                    moduleObject.on('click tap', function (evt) {
                            try {
                                if (command !== "") {
                                    var cmds = command + "CONTROLCMD" + dataKey + "CONTROLCMD" + "开关";
                                    eval("webSocketClilent.sendCmdWebSocket(cmds)");
                                } else {
                                    layui.layer.msg('点击命令未设置，不能触发！', {
                                        offset: 't',
                                        anim: 6,
                                        icon: 2
                                    });
                                }
                            
                            } catch (evt) {
                                layui.layer.msg('预览模式下未链接webservice服务端，不能触发！', {
                                    offset: 't',
                                    anim: 6,
                                    icon: 2
                                });
                            }
                    });



                    break;
                }

            }
        },
    /**
     *设置状态图片
     * @param moduleObject
     * @param valueObject
     */
        setImageStatusValue:function(moduleObject,valueObject,fieldStr){
        var layerObj = moduleObject.getLayer();
        var groupObj = moduleObject;

        var whereStr = "";
        var keyValue ="";
        var whereObj = [];
        var field=fieldStr?fieldStr.split('@')[0]:'';
        console.log(field);
        if(field!="thumb"){
        	whereStr = groupObj.attrs.where;
        	for (var item in valueObject) {
        	    keyValue =  valueObject[item];
        	}
        	
        	if( groupObj.temp_zttp_value == keyValue){
        	    return;
        	}else{
        	    groupObj.temp_zttp_value=keyValue;
        	}
        	
        	whereObj = eval("("+whereStr+")");
        }else{
        	keyValue = "default";
        	whereObj = [{
        		status:'default',
        		icon:baseURL+ valueObject[fieldStr]
        	}]
        }
        
		for (var i = 0; i < whereObj.length; i++) {
            var whereObjItem = whereObj[i];
            var status = whereObjItem.status;
            var icon = whereObjItem.icon;

            if(keyValue==status){
                //删除原有的 image 组件。
                var nodes = moduleObject.find('Image');
                nodes.each(function(node) {
                    node.destroy();
                });
                var urlImage = icon;

                if(urlImage.indexOf(".gif")==-1){
                    Konva.Image.fromURL(urlImage, function(imageNode){
                        imageNode.name("myShape");
                        imageNode.setAttr("offset",{x: imageNode.width() / 2,y: imageNode.height() / 2,});
                        moduleObject.add(imageNode);
                        layerObj.draw();
                    },"config/gyyj/noimg.svg");
                }else{
                    var canvas = document.createElement('canvas');
                    var image;
                    var statusIn_add=false;
                    if (groupObj.hasOwnProperty('gifletObj')) { //必须先停止
                        groupObj.gifletObj.stop();
                    }
                     gifler(urlImage).frames(canvas, function (ctx, frame) {
                            // update canvas size
                            canvas.width = frame.width;
                            canvas.height = frame.height;
                            // update canvas that we are using for Konva.Image
                            ctx.drawImage(frame.buffer, 0, 0);
                            // redraw the layer
                            stageView.layer.batchDraw();
                            if(!statusIn_add){
                                image = new Konva.Image({
                                    image: canvas,
                                    offset: {x: canvas.width / 2, y: canvas.height / 2}
                                });
                                moduleObject.add(image);
                                statusIn_add=true;
                            }



                        }
                    ).then(function(animator) {
                         groupObj.gifletObj = null;
                         groupObj.gifletObj = animator;
                    });
                }



                break;
            }

        }
    },
        /**
         * 旋转调用的方法
         * @param moduleObject
         * @param valueObject
         */
        rotateModule:function(moduleObject,valueObjectAll){
           var valueObjectAllStr =  JSON.stringify(valueObjectAll);
            var groupObj = moduleObject;
            var layerObj = groupObj.getLayer();
            var rotateWhere = groupObj.attrs.rotateWhere;

            var rotateWhereObj = eval("("+rotateWhere+")");
            var valueStrT="";
            for (var z = 0; z < rotateWhereObj.length; z++) {
                var whereObjItemT = rotateWhereObj[z];
                var devicecodeT = whereObjItemT.devicecode;
                var deviceValueT = valueObjectAll[devicecodeT];
                valueStrT+=deviceValueT;
            }
            if( groupObj.temp_xz_value == valueStrT){
                return;
            }else{
                groupObj.temp_xz_value=valueStrT;
            }



            var speed = groupObj.attrs.speed;
            var ismz = false;//是否满足动画条件
            var isUndefined=true;
            for (var i = 0; i < rotateWhereObj.length; i++) {
                var whereObjItem = rotateWhereObj[i];
                var devicecode = whereObjItem.devicecode;
                var min=whereObjItem.min;
                var max=whereObjItem.max;

                var deviceValue = valueObjectAll[devicecode];

                if(typeof(deviceValue)=="undefined"){
                    continue;
                }else{
                    isUndefined=false;// 只要要一个不等于 undefined 都是为正常
                }

                if(Number(deviceValue)>=Number(min)&&Number(deviceValue)<=Number(max)){
                    if(groupObj.hasOwnProperty('rotateAnim')) { //必须先停止
                        groupObj.rotateAnim.stop();
                    }

                    if(groupObj.hasOwnProperty('rotateAnim')) {
                        groupObj.rotateAnim.start();
                    }else{
                        groupObj.rotateAnim = new Konva.Animation(function (frame) {
                            groupObj.rotate(speed);

                            // var angleDiff = (frame.timeDiff * 90) / 1000;
                            // groupObj.rotate(angleDiff);


                        }, layerObj);
                        groupObj.rotateAnim.start();
                    }


                    ismz=true;
                    break;
                }
            }

            if(isUndefined){
                return;
            }

            if (ismz == false) {//

                try {//如果第一次就不满足动画，那么这里会抛出异常
                    groupObj.rotateAnim.stop();
                    //layerObj.draw();
                } catch (e) {

                }
            }
        },
        /**
         * 新旋转调用的方法 支持顺时针，逆时针旋转
         * @param moduleObject
         * @param valueObject
         */
        rotateModuleNew:function(moduleObject,valueObject){

            var groupObj = moduleObject;
            var layerObj = groupObj.getLayer();
            var whereStr = groupObj.attrs.rotateWhereNew;

            var keyValue ="";
            for (var item in valueObject) {
                keyValue =  valueObject[item];
            }

            if(groupObj.temp_rotateNew_value == keyValue){
                return;
            }else{
                groupObj.temp_rotateNew_value=keyValue;
            }


            var whereObj = eval("("+whereStr+")");
            for (var i = 0; i < whereObj.length; i++) {
                var whereObjItem = whereObj[i];
                var directionRotation = whereObjItem.directionRotation;
                var min=whereObjItem.min;
                var max=whereObjItem.max;
                if(Number(keyValue)>=Number(min)&&Number(keyValue)<=Number(max)){

                    if(directionRotation==1){//顺时针旋转
                        if (groupObj.hasOwnProperty('anim')) { //必须先停止
                            groupObj.anim.stop();
                        }
                        groupObj.anim = new Konva.Animation(function (frame) {
                            var angularSpeed = 150;//没秒100弧度旋转
                            var angleDiff = (angularSpeed * frame.timeDiff) / 1000;
                            groupObj.rotate(angleDiff);
                        }, layerObj);
                        groupObj.anim.start();
                    }
                    else if(directionRotation==11){//顺时针旋转 快
                        if (groupObj.hasOwnProperty('anim')) { //必须先停止
                            groupObj.anim.stop();
                        }
                        groupObj.anim = new Konva.Animation(function (frame) {
                            var angularSpeed = 300;//没秒300弧度旋转
                            var angleDiff = (angularSpeed * frame.timeDiff) / 1000;
                            groupObj.rotate(angleDiff);
                        }, layerObj);
                        groupObj.anim.start();
                    }
                    else if(directionRotation==2){//逆时针旋转
                        if (groupObj.hasOwnProperty('anim')) { //必须先停止
                            groupObj.anim.stop();
                        }
                        groupObj.anim = new Konva.Animation(function (frame) {
                            var angularSpeed = -150;//没秒100弧度旋转
                            var angleDiff = (angularSpeed * frame.timeDiff) / 1000;
                            groupObj.rotate(angleDiff);
                        }, layerObj);
                        groupObj.anim.start();
                    }
                    else if(directionRotation==22){//逆时针旋转 快
                        if (groupObj.hasOwnProperty('anim')) { //必须先停止
                            groupObj.anim.stop();
                        }
                        groupObj.anim = new Konva.Animation(function (frame) {
                            var angularSpeed = -300;//没秒300弧度旋转
                            var angleDiff = (angularSpeed * frame.timeDiff) / 1000;
                            groupObj.rotate(angleDiff);
                        }, layerObj);
                        groupObj.anim.start();
                    }

                    break;
                }else{
                    if (groupObj.hasOwnProperty('anim')) { //必须先停止
                        groupObj.anim.stop();
                    }
                }




            }
        },
        /**
         * 隐藏
         * @param moduleObject
         * @param valueObjectAll
         */
        hideModule:function(moduleObject,valueObjectAll){
            var groupObj = moduleObject;
            var layerObj = groupObj.getLayer();
            var hideWhere = groupObj.attrs.hideWhere;

            var hideWhereObj = eval("("+hideWhere+")");
            var valueStrT="";
            for (var z = 0; z < hideWhereObj.length; z++) {
                var whereObjItemT = hideWhereObj[z];
                var devicecodeT = whereObjItemT.devicecode;
                var deviceValueT = valueObjectAll[devicecodeT];
                valueStrT+=deviceValueT;
            }
            if( groupObj.temp_yc_value == valueStrT){
                return;
            }else{
                groupObj.temp_yc_value=valueStrT;
            }




            var ismz = false;//是否满足动画条件
            var isUndefined=true;
            for (var i = 0; i < hideWhereObj.length; i++) {
                var whereObjItem = hideWhereObj[i];
                var devicecode = whereObjItem.devicecode;
                var min=whereObjItem.min;
                var max=whereObjItem.max;
                var deviceValue = valueObjectAll[devicecode];

                if(typeof(deviceValue)=="undefined"){
                    continue;
                }else{
                    isUndefined=false;// 只要要一个不等于 undefined 都是为正常
                }

                if(Number(deviceValue)>=Number(min)&&Number(deviceValue)<=Number(max)){
                    groupObj.hide();
                    //layerObj.draw();
                    ismz=true;
                    break;
                }
            }
            if(isUndefined){
                return;
            }
            if (ismz == false) {//

                try {//如果第一次就不满足动画，那么这里会抛出异常
                    groupObj.show();
                    //layerObj.draw();
                } catch (e) {

                }
            }
        },
        /**
         * 闪烁方法
         * @param moduleObject
         * @param valueObjectAll
         */
        sparklingModule:function(moduleObject,valueObjectAll){
            var groupObj = moduleObject;
            var layerObj = groupObj.getLayer();
            var sparklingWhere = groupObj.attrs.sparklingWhere;

            var sparklingWhereObj = eval("("+sparklingWhere+")");
            var valueStrT="";
            for (var z = 0; z < sparklingWhereObj.length; z++) {
                var whereObjItemT = sparklingWhereObj[z];
                var devicecodeT = whereObjItemT.devicecode;
                var deviceValueT = valueObjectAll[devicecodeT];
                valueStrT+=deviceValueT;
            }
            if( groupObj.temp_ss_value == valueStrT){
                return;
            }else{
                groupObj.temp_ss_value=valueStrT;
            }



            var ismz = false;//是否满足动画条件
            var isUndefined=true;
            for (var i = 0; i < sparklingWhereObj.length; i++) {
                var whereObjItem = sparklingWhereObj[i];
                var devicecode = whereObjItem.devicecode;
                var min=whereObjItem.min;
                var max=whereObjItem.max;
				//console.log(min);
				//console.log(max);
                var deviceValue = valueObjectAll[devicecode];
                if(valueObjectAll[devicecode]===null || valueObjectAll[devicecode]==="null"){
                	deviceValue="null";
                }
                if(typeof(deviceValue)=="undefined"){
                    continue;
                }else{
                    isUndefined=false;// 只要要一个不等于 undefined 都是为正常
                }

                if((Number(deviceValue)>=Number(min)&&Number(deviceValue)<=Number(max)) || (deviceValue==min&&deviceValue==max && deviceValue=="null")){

                    if(groupObj.hasOwnProperty('sparklingTween')) { //必须先停止
                        groupObj.sparklingTween.reset();
                        groupObj.sparklingTween.reverse();
                        groupObj.sparklingTween.play();
                    }else{
                    groupObj.sparklingTween = new Konva.Tween({
                        node: groupObj,
                        duration: 1,
                        opacity: 0,
                        // easing:Konva.Easings.ElasticEaseInOut,
                        onFinish: function () {
                            groupObj.sparklingTween.reset();
                            groupObj.sparklingTween.reverse();
                            groupObj.sparklingTween.play();
                        }
                    });
                    groupObj.sparklingTween.play();
                    }




                    ismz=true;
                    break;
                }
            }

            if(isUndefined){
                return;
            }

            if (ismz == false) {//

                try {//如果第一次就不满足动画，那么这里会抛出异常
                    groupObj.sparklingTween.reset();
                    groupObj.sparklingTween.reverse();
                } catch (e) {

                }
            }
        }



    }
    win.ModuleAnimation = ModuleAnimation;
}(window);
